package cmd

import (
	"BookStack/internal/domain/category"
	"fmt"
	"log"
	"time"

	"gorm.io/gorm"
)

// CreateSampleCategories 创建示例分类
func CreateSampleCategories(db *gorm.DB) error {
	log.Println("开始创建示例分类...")

	// 检查是否已存在分类
	var count int64
	if err := db.Model(&category.Category{}).Count(&count).Error; err != nil {
		return fmt.Errorf("检查分类数量失败: %v", err)
	}
	if count > 0 {
		log.Println("分类已存在，跳过创建示例分类")
		return nil
	}

	// 创建文章分类
	articleCategories := []*category.Category{
		{
			Name:        "技术文章",
			Slug:        "tech-articles",
			Type:        1,
			Description: "技术相关文章",
			Order:       1,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "编程语言",
			Slug:        "programming-languages",
			Type:        1,
			Description: "各种编程语言相关文章",
			Order:       2,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "Web开发",
			Slug:        "web-development",
			Type:        1,
			Description: "Web开发相关文章",
			Order:       3,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "数据库",
			Slug:        "database",
			Type:        1,
			ParentID:    1,
			Description: "数据库相关的文章",
			Order:       3,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "生活随笔",
			Slug:        "life-essays",
			Type:        1,
			Description: "生活相关的文章分类",
			Order:       2,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "旅行日记",
			Slug:        "travel-diary",
			Type:        1,
			ParentID:    5,
			Description: "旅行相关的文章",
			Order:       1,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "美食分享",
			Slug:        "food-sharing",
			Type:        1,
			ParentID:    5,
			Description: "美食相关的文章",
			Order:       2,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
	}

	// 创建书籍分类
	bookCategories := []*category.Category{
		{
			Name:        "技术书籍",
			Slug:        "tech-books",
			Type:        2,
			Description: "技术相关书籍",
			Order:       1,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "编程语言",
			Slug:        "programming-languages-books",
			Type:        2,
			Description: "各种编程语言相关书籍",
			Order:       2,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "Web开发",
			Slug:        "web-development-books",
			Type:        2,
			ParentID:    8,
			Description: "Web开发相关的图书",
			Order:       2,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "数据库",
			Slug:        "database-books",
			Type:        2,
			ParentID:    8,
			Description: "数据库相关的图书",
			Order:       3,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "文学小说",
			Slug:        "literature-novels",
			Type:        2,
			Description: "文学小说类书籍",
			Order:       3,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "科幻小说",
			Slug:        "sci-fi-novels",
			Type:        2,
			ParentID:    12,
			Description: "科幻小说相关的图书",
			Order:       1,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
		{
			Name:        "武侠小说",
			Slug:        "martial-arts-novels",
			Type:        2,
			ParentID:    12,
			Description: "武侠小说相关的图书",
			Order:       2,
			Status:      1,
			CreatedAt:   time.Now(),
			UpdatedAt:   time.Now(),
		},
	}

	// 创建文章分类
	for _, category := range articleCategories {
		if err := db.Create(category).Error; err != nil {
			return fmt.Errorf("创建文章分类失败: %v", err)
		}
	}

	// 创建书籍分类
	for _, category := range bookCategories {
		if err := db.Create(category).Error; err != nil {
			return fmt.Errorf("创建书籍分类失败: %v", err)
		}
	}

	log.Println("示例分类创建成功")
	return nil
}
